home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / wdj0796.zip / TOMLINSN.ZIP / SRVCMD.C
C/C++ Source or Header  |  1996-04-09  |  4KB  |  123 lines

  1. // srvcmd.c
  2.  
  3. #include <windows.h>
  4. #include <tchar.h>
  5. #include <stdio.h>
  6.  
  7. VOID DisplayInstructions(VOID);
  8.  
  9. BOOL ServiceCommands(int argc, char **argv,
  10.       LPTSTR szServiceName, LPTSTR szDisplayName)
  11. {
  12.    SC_HANDLE   hSCManager, hService;
  13.    TCHAR szPath[MAX_PATH];
  14.  
  15.    if (argc <= 1) return FALSE;
  16.  
  17.    if ((*argv[1] == '-') || (*argv[1] == '/')) {
  18.  
  19.       if (lstrcmpi(argv[1]+1, "install") == 0) {
  20.  
  21.          //------------------------------------------
  22.          // install the service
  23.          //-------------------------------------------
  24.  
  25.          if ((hSCManager = OpenSCManager(NULL, NULL,
  26.                SC_MANAGER_ALL_ACCESS))) {
  27.  
  28.             GetModuleFileName(NULL, szPath, MAX_PATH);
  29.  
  30.             if ((hService = CreateService(hSCManager,
  31.                   szServiceName, szDisplayName,
  32.                   SERVICE_ALL_ACCESS,
  33.                   SERVICE_WIN32_OWN_PROCESS,
  34.                   SERVICE_DEMAND_START,
  35.                   SERVICE_ERROR_NORMAL,
  36.                   szPath, NULL, NULL, NULL, NULL, NULL))) {
  37.  
  38.                 CloseServiceHandle(hService);
  39.                _tprintf(TEXT("Service installed.\n"));
  40.             } else {
  41.                _tprintf(TEXT("Installation failed\n"));
  42.             }
  43.  
  44.             CloseServiceHandle(hSCManager);
  45.          } else {
  46.             _tprintf(TEXT("Installation failed\n"));
  47.          }
  48.       }
  49.  
  50.       else if (lstrcmpi(argv[1]+1, "remove") == 0) {
  51.  
  52.          //-------------------------------------------
  53.          // Remove the service
  54.          //-------------------------------------------
  55.  
  56.          if ((hSCManager = OpenSCManager(NULL, NULL,
  57.                SC_MANAGER_ALL_ACCESS))) {
  58.  
  59.             SERVICE_STATUS ss;
  60.  
  61.             if ((hService = OpenService(hSCManager,
  62.                   szServiceName, SERVICE_ALL_ACCESS))) {
  63.  
  64.                // Attempt to stop the service
  65.                if (ControlService(hService,
  66.                      SERVICE_CONTROL_STOP, &ss)) {
  67.  
  68.                   _tprintf(TEXT("Stopping the service."));
  69.                   Sleep(2000);
  70.  
  71.                   while(QueryServiceStatus(hService, &ss)) {
  72.                      if (ss.dwCurrentState ==
  73.                              SERVICE_STOP_PENDING) {
  74.                          _tprintf(TEXT("."));
  75.                          Sleep(2000);
  76.                      } else {
  77.                          break;
  78.                      }
  79.                   }
  80.  
  81.                   if (ss.dwCurrentState == SERVICE_STOPPED) {
  82.                      _tprintf(TEXT("\nService stopped.\n"));
  83.                   } else {
  84.                      _tprintf(TEXT("\nRemove failed.\n"));
  85.                   }
  86.                }
  87.  
  88.                // service is stopped, remove it
  89.                if(DeleteService(hService)) {
  90.                   _tprintf(TEXT("Service removed.\n"));
  91.                } else {
  92.                   _tprintf(TEXT("Remove failed.\n"));
  93.                }
  94.  
  95.                CloseServiceHandle(hService);
  96.  
  97.             } else {
  98.                CloseServiceHandle(hSCManager);
  99.                _tprintf(TEXT("Remove failed.\n"));
  100.             }
  101.          } else {
  102.            _tprintf(TEXT("Remove failed.\n"));
  103.          }
  104.  
  105.       } else {
  106.          DisplayInstructions();
  107.       }
  108.    } else {
  109.        DisplayInstructions();
  110.    }
  111.  
  112.    return TRUE;
  113. }
  114.  
  115.  
  116. VOID DisplayInstructions(VOID)
  117. {
  118.    printf("This service supports the following commands:\n");
  119.    printf("   -install  (Installs the service)\n");
  120.    printf("   -remove   (Remove the service)\n");
  121. }
  122.  
  123.